<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>角色增加</title>
    <script src="../../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../../res/js/Tools.js" charset="utf-8"></script>
    <script src="../../../../../res/js/error.js" charset="utf-8"></script>
    <link rel="shortcut icon" type="image/x-icon" href="../../../../../res/images/favicon.ico">
    <link rel="stylesheet" href="../../../../../res/layui/css/layui.css" media="all">
</head>
<body>
<form action="" class="layui-form layui-form-pane">
    <input type="hidden" id="pkRoleId" name="pkRoleId">
    <div class="layui-form-item">
        <label class="layui-form-label">角色名</label>
        <div class="layui-input-block">
            <input autocomplete="off" class="layui-input" lay-reqtext="角色名是必填项，岂能为空？" lay-verify="required"
                   name="roleName" id="roleName" placeholder="请输入(必填项)" type="text">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">部门 </label>
        <div class="layui-input-inline">
            <select lay-filter="organizationId" name="roleInherit" id="organizationId" >
                <option selected="" value="">部门</option>
            </select>
        </div>
        <label class="layui-form-label">岗位 </label>
        <div class="layui-input-inline">
            <select lay-filter="aihao" name="roleSuper" id="organizationType" >
                <option selected="" value="">岗位</option>
            </select>
        </div>

    </div>
    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">角色描述</label>
        <div class="layui-input-block">
            <textarea class="layui-textarea" name="roleDescribe" id="roleDescribe" placeholder="请输入描述"></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>

        <span id="msg"></span>
        <div class="layui-input-block">
            <div id="pkJurisdictionId" class="xm-select-demo"></div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn site-demo-active" id="sub" lay-filter="de1" lay-submit="" type="submit">立即提交</button>
        </div>
    </div>
</form>
<script>
    $.ajaxSetup({
        async: false
    });
    var form=null;
    var pkJurisdictionId ;
    layui.use(['form', 'layedit', 'laydate', 'layer'], function () {
        form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate;
        //加载部门下拉框
        const organizationId=getAjaxById(getRootPath()+"OaDepartmentAction/departmentfindByAll");
        $("#organizationId").html('<option value="">请选择部门</option>');
        $.each(organizationId.data,function (index,item) {
            $("#organizationId").append('<option value="'+item.pkDepartmentId+'">'+item.departmentName+'</option>')
        });
        //由部门选择岗位
        form.on('select(organizationId)', function(data){
            if(data.value){
                //加载岗位下拉框
                const organizationType=getAjaxById(getRootPath()+"OaOrganizationAction/organizationFind/"+data.value);
                $("#organizationType").html('<option value="">请选择岗位</option>');
                $.each(organizationType.data,function (index,item) {
                    $("#organizationType").append('<option value="'+item.pkPostId+'">'+item.postName+'</option>')
                });
            }else{
                $("#organizationType").html('<option value="">请选择岗位</option>');
            }
            form.render();
        });
        //修改操作
        var ide = getQueryString("pkRoleId");

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });
        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath()+'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function(){
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
             pkJurisdictionId = xmSelect.render({
                el: '#pkJurisdictionId',
                tree: {
                    show: true,
                    expandedKeys: true,
                },
                height: 'auto',
                data: loadJurisdictions(ide)
            })

        });
        //监听提交
        form.on('submit(de1)', function (data) {
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
           var selectArr=pkJurisdictionId.getValue();
            if (selectArr.length===0){
                layer.msg("权限不能为空,请选择", {icon: 2});
                return false;
            }
            //定义一个数组
            var ids = [];

           for (var i = 0; i < selectArr.length; i++) {
                   ids.push(selectArr[i].value);
                   ids.push(selectArr[i].superId);
           }
            for (let i = 0; i <ids.length-1; i++) {
                for (let j =i+1; j <ids.length; j++) {
                    if (ids[i]==ids[j]){
                        ids.splice(j,1);
                        j--;
                    }
                }

            }

           data.field.ids=ids;
             //去除select
            delete  data.field.select;
            var url=getRootPath() + "OaRoleAction/addOrUpdateRole";
            let res=postAjax(url,data.field);
            /*
            if ( $("#roleInherit").val()==$("#roleSuper").val()){
                layer.msg("继承角色不可以与上级角色相同", {icon: 2});
                return  false;
            }*/
            if(res.code===1){
                parent.layui.table.reload('test');//刷新父页面
                layer.msg("操作成功", {icon: 1});
                const index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            }else{
                layer.msg("操作失败");
            }
            return false;
        });

        //表单取值
        layui.$('#LAY-component-form-getval').on('click', function () {
            var data = form.val('example');
        });
      /*  loadRole(ide);*/

        if (ide) {
            findById(token);//修改页面的赋值
        }
        //重新渲染表单函数
        function renderForm() {
            layui.use('form', function () {
                var form = layui.form(); //高版本建议把括号去掉，有的低版本，需要加()
                form.render();
            });
        }

        form.render();
    });

    function findById(token) {
        var id = getQueryString("pkRoleId");
        loadJurisdictions(id);
        var url = getRootPath() + "/OaRoleAction/selectById/" + id;
        var data=getAjaxById(url);
        $("#pkRoleId").val(data.data.pkRoleId);
        $("#roleName").val(data.data.roleName);
        $("#organizationId").val(data.data.roleInherit);
        $("#roleDescribe").val(data.data.roleDescribe);
        if (data.data.roleInherit){
            url =getRootPath()+"OaOrganizationAction/organizationFind/"+data.data.roleInherit;
            const datas=getAjaxById(url);
            $("#organizationType").html('<option value="">请选择岗位</option>');
            $.each(datas.data,function(index,item){
                $("#organizationType").append("<option value='"+item.pkPostId+"'>"+item.postName+"</option>");
            });
            $("#organizationType").val(data.data.roleSuper);
        }
                layui.form.render("select");

    }
   /* function loadRole(id){
        let url;
        if(id){
            url=getRootPath()+"OaRoleAction/roleFindByAll/"+id;
        }else{
            url=getRootPath()+"OaRoleAction/roleFindByAll/0";
        }
        var data=getAjaxById(url);
      /!*  $.each(data.data,function(index,item){
            /!*   $("#roleInherit").append("<option value='"+item.pkRoleId+"'>"+item.roleName+"</option>");*!/
            $("#roleSuper").append("<option value='"+item.pkRoleId+"'>"+item.roleName+"</option>");
        });*!/
    }*/
    $(function(){
        //给文本框绑定失去焦点的事件
        $("#pkJurisdictionId").blur(function(){
            var message = '';
            var selectArr=pkJurisdictionId.getValue();
            if ( selectArr==null&& selectArr.length>0){
                message ="不能为空，请重新输入";
                $("#msg").css("color","red").html(message);
            }else {
                message ="";
            }
        })
    })

    
    function loadJurisdictions(ide){
        if(ide){
            var url=getRootPath()+"OaJurisdictionAction/findByIds/"+ide;
            var datas=getAjaxById(url);
            return datas;
        }else{
            var url=getRootPath()+"OaJurisdictionAction/findAlls";
            var datas=getAjaxById(url);
            return datas;
        }

    }

</script>
</body>
</html>